package it.unibo.refolding.pico;

import it.unibo.refolding.common.CommonThings;
import it.unibo.refolding.common.Sleeper;
import it.unimi.dsi.fastutil.doubles.DoubleList;

import java.nio.file.Path;

public abstract class AbstractRefoldingNanoAdapter implements RefoldingNanoAdapter {

	private static final double DEFAULT_VELOCITY_M_OVER_S = 2e-6;

	private final double[] revVels;
	protected final double defaultRevVelMPerS;
	protected final double breakAwayRampSizeM;
	protected final double fetchMoleculeRampSizeM;
	protected final double fetchMoleculeSurfaceDelayS;
	protected final double loseRampSizeM;
	protected final double loseVelocityMPerS;
	protected final DoubleList allowedTimeRelativeError;
	protected final Path junctionPointsDir;
	protected final Sleeper sleeper;
	protected final Sleeper nanoDriverSleeper;
	protected final FileMovingWatcher fileMovingWatcher;

	protected NanoDriver nanoDriver;

	protected AbstractRefoldingNanoAdapter(double[] revVels, double breakAwayRampSizeM,
		double fetchMoleculeRampSizeM, double fetchMoleculeSurfaceDelayS,
		double loseRampSizeM, double loseVelocityMPerS, DoubleList allowedTimeRelativeError,
		Path junctionPointsDir, Sleeper sleeper, Sleeper nanoDriverSleeper, FileMovingWatcher fileMovingWatcher) {
		this.revVels = revVels;
		this.breakAwayRampSizeM = breakAwayRampSizeM;
		this.fetchMoleculeRampSizeM = fetchMoleculeRampSizeM;
		this.fetchMoleculeSurfaceDelayS = fetchMoleculeSurfaceDelayS;
		this.loseRampSizeM = loseRampSizeM;
		this.loseVelocityMPerS = loseVelocityMPerS;
		this.allowedTimeRelativeError = allowedTimeRelativeError;
		this.junctionPointsDir = junctionPointsDir;
		this.sleeper = sleeper;
		this.nanoDriverSleeper = nanoDriverSleeper;
		this.fileMovingWatcher = fileMovingWatcher;
		this.defaultRevVelMPerS = revVels.length == 0 ? DEFAULT_VELOCITY_M_OVER_S : CommonThings.max(revVels);
	}

	@Override
	public void setNanoDriver(NanoDriver nanoDriver) {
		this.nanoDriver = nanoDriver;
	}

	protected double reverseVelocity(int index) {
		return revVels[index];
	}
}
